﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;


namespace Ewts.ashx
{
    /// <summary>
    /// StartStorage 的摘要说明
    /// </summary>
    public class StartStorage : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (context.Request["delbarid"] != null)
            {
                if(DelBarCode(int.Parse(context.Request["delbarid"].ToString())))
                       context.Response.Write("1");
                else
                    context.Response.Write("-1");
            }
            else
            {
                string json = AddStartStorage(context);
                context.Response.Write(json);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 库存盘点
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public string AddStartStorage(HttpContext context)
        {
            DataLib.BLL.BarCode bllBar = new DataLib.BLL.BarCode();
            DataLib.Model.BarCode mBar = new DataLib.Model.BarCode();

            string json = "";
            DataLib.Model.BarCode model = new DataLib.Model.BarCode();
            model.id = getNumberByobj(context.Request["barid"]);
            model.goodsid = getNumberByobj(context.Request["goodsid"]);
            model.storageid = getNumberByobj(context.Request["storageid"]);
            model.InStrorageID = getNumberByobj(context.Request["insid"]);
            model.loadnumid = getNumberByobj(context.Request["loadid"]);
            model.cjoid = getNumberByobj(context.Request["cjoid"]);
            model.qty = getNumberByobj(context.Request["qty"]);

            model.goodsid = getNumberByobj(context.Request["goodsid"]);
            model.goodscode = getObjTostring(context.Request["gcode"]);
            model.goodsname = getObjTostring(context.Request["gname"]);
            model.stroageNo = getObjTostring(context.Request["stroageno"]);
            model.barbelong = getObjTostring(context.Request["bcity"]);
            model.bargraphic = getObjTostring(context.Request["gcity"]);
            model.barcodevalue = getObjTostring(context.Request["barvalue"]);
            model.createdate = DateTime.Parse(context.Request["pddate"].ToString());
            model.remark = getObjTostring(context.Request["operman"]);

            model.gwt = getDecimalByobj(context.Request["gwt"]);
            model.tare1 = getDecimalByobj(context.Request["tare1"]);
            model.tare2 = getDecimalByobj(context.Request["tare2"]);
            model.qty = getNumberByobj(context.Request["qty"]);
            model.packageno = getObjTostring(context.Request["boxno"]);
            model.boxcount = getNumberByobj(context.Request["boxcount"]);
            model.rubbishcode = getObjTostring(context.Request["rcode"]);
            model.rubbishtype = getObjTostring(context.Request["rtype"]);
            model.remark = getObjTostring(context.Request["remark"]);
            model.rdh = getObjTostring(context.Request["rdh"]);
            model.StorageArea = getObjTostring(context.Request["month"]);
            mBar = new DataLib.BLL.BarCode().InsertBarcode(model);
            if (mBar != null)
            {
                
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                json = serializer.Serialize(mBar);
            }
            

            return json;
        }

        /// <summary>
        /// 直接删除BarCode
        /// </summary>
        public bool DelBarCode(int barid)
        {
            //更新库存
            DataLib.BLL.BarCode bll = new DataLib.BLL.BarCode();
            return bll.Delete(barid);
        }


        #region
        /// <summary>
        /// 当对象为空返回 "" 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string getObjTostring(object obj)
        {
            if (obj != null && obj.ToString().Trim() != "")
                return obj.ToString().Trim();
            else
                return "";
        }

        /// <summary>
        /// 当字符串为空 返回默认的值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="def"></param>
        /// <returns></returns>
        public string getObjTostring(object obj, string def)
        {
            string str = getObjTostring(obj);
            if (str == "")
                str = def;
            return str;
        }

        /// <summary>
        /// 当对象为空，返回0
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int getNumberByobj(object obj)
        {
            if (obj != null && obj.ToString().Trim() != "")
                return int.Parse(obj.ToString().Trim());
            else
                return 0;
        }

        /// <summary>
        /// 当对象为空，返回0
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public decimal getDecimalByobj(object obj)
        {
            if (obj != null && obj.ToString().Trim() != "")
                return decimal.Parse(obj.ToString().Trim());
            else
                return 0;
        }

        #endregion

    }
}